package com.siun.dk.base;

import static com.siun.dk.constants.MyConstant.NOTIFY_CHANNEL_ID;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;

import androidx.multidex.BuildConfig;

import com.aw.baselib.base.AwBaseApplication;
import com.aw.baselib.util.AwCrashUtil;
import com.aw.baselib.util.AwLog;
import com.sirun.ble.listeners.OnBleInitListener;
import com.sirun.ble.manager.BleManager;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author : hzw
 */
public class MyApp extends AwBaseApplication {

    private static MyApp instance;
    public boolean isBleInitSuccess = false;


    public static MyApp getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        initBle();
//        initPush();
        AwLog.setDebug(true);
        AwLog.setLogFile(true);
        createNotificationChannel();
        AwCrashUtil.getInstance()
                .setDebugMode(BuildConfig.DEBUG)
                .setMainCrashHandler((t, e) -> {
                    StringWriter stringWriter = new StringWriter();
                    e.printStackTrace(new PrintWriter(stringWriter,true));
                    AwLog.d("APP", "MainCrashHandler: " + e.getMessage());
                    AwLog.d("APP", "MainCrashHandler: " + stringWriter.toString());
                    //todo 跨线程操作时注意线程调度回主线程操作
                    //todo 此处做你的日志记录即可
                })
                .setUncaughtCrashHandler((t, e) -> {
                    //todo 跨线程操作时注意线程调度回主线程操作
                    com.aw.baselib.util.AwLog.d("AwCrash子线程异常");//此处log只是展示，当debug为true时，主类内部log会打印异常信息
                    StringWriter stringWriter = new StringWriter();
                    e.printStackTrace(new PrintWriter(stringWriter,true));
                    AwLog.d("APP", "UncaughtCrashHandle: " + e.getMessage());
                    AwLog.d("APP", "UncaughtCrashHandle: " + stringWriter.toString());
                    //todo 此处做你的日志记录即可
                })
                .register(this);
    }


    private void initBle() {
        BleManager.getInstance().initBleManager(MyApp.getInstance(), new OnBleInitListener() {
            @Override
            public void initBleSuccess() {
                AwLog.d("===>initAwBleManager success");
                isBleInitSuccess = true;
            }

            @Override
            public void initBleFail(int code, String msg) {
                AwLog.d("===>initAwBleManager fail, code: " + code + " ,msg: " + msg);
                isBleInitSuccess = false;
            }
        });
    }

    private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "zhidou";
            String description = "zhidou_desc";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(NOTIFY_CHANNEL_ID, name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

    @Override
    public void needReLogin() {

    }

    @Override
    public boolean isAllowShowReloginDialog() {
        return true;
    }
}
